{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: ollama in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (0.2.0)\n",
      "Requirement already satisfied: gspread in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (6.1.2)\n",
      "Requirement already satisfied: pandas in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (2.1.1)\n",
      "Requirement already satisfied: httpx<0.28.0,>=0.27.0 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from ollama) (0.27.0)\n",
      "Requirement already satisfied: google-auth>=1.12.0 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from gspread) (2.29.0)\n",
      "Requirement already satisfied: google-auth-oauthlib>=0.4.1 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from gspread) (1.2.0)\n",
      "Requirement already satisfied: numpy>=1.22.4 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from pandas) (1.26.0)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from pandas) (2023.3.post1)\n",
      "Requirement already satisfied: tzdata>=2022.1 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from pandas) (2023.3)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from google-auth>=1.12.0->gspread) (5.3.1)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from google-auth>=1.12.0->gspread) (0.4.0)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from google-auth>=1.12.0->gspread) (4.9)\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from google-auth-oauthlib>=0.4.1->gspread) (2.0.0)\n",
      "Requirement already satisfied: anyio in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from httpx<0.28.0,>=0.27.0->ollama) (4.4.0)\n",
      "Requirement already satisfied: certifi in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from httpx<0.28.0,>=0.27.0->ollama) (2023.7.22)\n",
      "Requirement already satisfied: httpcore==1.* in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from httpx<0.28.0,>=0.27.0->ollama) (1.0.5)\n",
      "Requirement already satisfied: idna in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from httpx<0.28.0,>=0.27.0->ollama) (3.4)\n",
      "Requirement already satisfied: sniffio in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from httpx<0.28.0,>=0.27.0->ollama) (1.3.1)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from httpcore==1.*->httpx<0.28.0,>=0.27.0->ollama) (0.14.0)\n",
      "Requirement already satisfied: six>=1.5 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
      "Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from pyasn1-modules>=0.2.1->google-auth>=1.12.0->gspread) (0.6.0)\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (3.2.2)\n",
      "Requirement already satisfied: requests>=2.0.0 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (2.31.0)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from anyio->httpx<0.28.0,>=0.27.0->ollama) (1.1.3)\n",
      "Requirement already satisfied: typing-extensions>=4.1 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from anyio->httpx<0.28.0,>=0.27.0->ollama) (4.8.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (3.3.0)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/Caskroom/miniforge/base/envs/codecarbon/lib/python3.9/site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib>=0.4.1->gspread) (2.0.6)\n"
     ]
    }
   ],
   "source": [
    "!pip install ollama gspread pandas\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ollama\n",
    "from codecarbon import EmissionsTracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': 'success'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelfile='''\n",
    "FROM llama2-uncensored\n",
    "SYSTEM You are a poet but you like to keep it simple\n",
    "PARAMETER temperature 5\n",
    "'''\n",
    "\n",
    "ollama.create(model='llama2-uncensored:hot', modelfile=modelfile)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate Poems and track emissions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[codecarbon INFO @ 17:00:51] [setup] RAM Tracking...\n",
      "[codecarbon INFO @ 17:00:51] [setup] GPU Tracking...\n",
      "[codecarbon INFO @ 17:00:51] No GPU found.\n",
      "[codecarbon INFO @ 17:00:51] [setup] CPU Tracking...\n",
      "[codecarbon INFO @ 17:00:52] Tracking Apple CPU and GPU via PowerMetrics\n",
      "[codecarbon INFO @ 17:00:52] >>> Tracker's metadata:\n",
      "[codecarbon INFO @ 17:00:52]   Platform system: macOS-12.6-arm64-arm-64bit\n",
      "[codecarbon INFO @ 17:00:52]   Python version: 3.9.18\n",
      "[codecarbon INFO @ 17:00:52]   CodeCarbon version: 2.4.3rc1\n",
      "[codecarbon INFO @ 17:00:52]   Available RAM : 8.000 GB\n",
      "[codecarbon INFO @ 17:00:52]   CPU count: 8\n",
      "[codecarbon INFO @ 17:00:52]   CPU model: Apple M1\n",
      "[codecarbon INFO @ 17:00:52]   GPU count: 1\n",
      "[codecarbon INFO @ 17:00:52]   GPU model: Apple M1\n",
      "[codecarbon INFO @ 17:00:53] ApiClient Successfully registered your run on the API.\n",
      "\n",
      "Run ID: 934a85fd-2b20-4666-aa06-f8b9b002c0c1\n",
      "Experiment ID: c7ddaa20-db55-411e-aeab-49c2fed4341e\n",
      "\n",
      "[codecarbon INFO @ 17:01:08] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.005687713623046875 W\n",
      "[codecarbon INFO @ 17:01:10] Energy consumed for all CPUs : 0.000005 kWh. Total CPU Power : 1.2865000000000002 W\n",
      "[codecarbon INFO @ 17:01:13] Energy consumed for all GPUs : 0.000030 kWh. Total GPU Power : 5.9243 W\n",
      "[codecarbon INFO @ 17:01:15] 0.000036 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:01:23] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.0057048797607421875 W\n",
      "[codecarbon INFO @ 17:01:25] Energy consumed for all CPUs : 0.000006 kWh. Total CPU Power : 0.29280000000000006 W\n",
      "[codecarbon INFO @ 17:01:29] Energy consumed for all GPUs : 0.000051 kWh. Total GPU Power : 6.192800000000001 W\n",
      "[codecarbon INFO @ 17:01:31] 0.000057 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:01:38] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.0042057037353515625 W\n",
      "[codecarbon INFO @ 17:01:40] Energy consumed for all CPUs : 0.000007 kWh. Total CPU Power : 0.5155999999999998 W\n",
      "[codecarbon INFO @ 17:01:43] Energy consumed for all GPUs : 0.000066 kWh. Total GPU Power : 5.276700000000001 W\n",
      "[codecarbon INFO @ 17:01:44] 0.000073 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:01:53] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.0039768218994140625 W\n",
      "[codecarbon INFO @ 17:01:55] Energy consumed for all CPUs : 0.000011 kWh. Total CPU Power : 1.7186999999999997 W\n",
      "[codecarbon INFO @ 17:01:59] Energy consumed for all GPUs : 0.000084 kWh. Total GPU Power : 5.1347 W\n",
      "[codecarbon INFO @ 17:02:01] 0.000096 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:02:08] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.00382232666015625 W\n",
      "[codecarbon INFO @ 17:02:10] Energy consumed for all CPUs : 0.000019 kWh. Total CPU Power : 3.3158000000000003 W\n",
      "[codecarbon INFO @ 17:02:13] Energy consumed for all GPUs : 0.000100 kWh. Total GPU Power : 5.154100000000001 W\n",
      "[codecarbon INFO @ 17:02:15] 0.000118 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:02:23] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.00514984130859375 W\n",
      "[codecarbon INFO @ 17:02:25] Energy consumed for all CPUs : 0.000022 kWh. Total CPU Power : 1.5976000000000001 W\n",
      "[codecarbon INFO @ 17:02:30] Energy consumed for all GPUs : 0.000116 kWh. Total GPU Power : 4.448600000000001 W\n",
      "[codecarbon INFO @ 17:02:34] 0.000138 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:02:38] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.0036506652832031254 W\n",
      "[codecarbon INFO @ 17:02:40] Energy consumed for all CPUs : 0.000023 kWh. Total CPU Power : 0.6014999999999999 W\n",
      "[codecarbon INFO @ 17:02:43] Energy consumed for all GPUs : 0.000128 kWh. Total GPU Power : 6.141399999999999 W\n",
      "[codecarbon INFO @ 17:02:44] 0.000151 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:02:53] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.00382232666015625 W\n",
      "[codecarbon INFO @ 17:02:55] Energy consumed for all CPUs : 0.000024 kWh. Total CPU Power : 0.6036 W\n",
      "[codecarbon INFO @ 17:02:58] Energy consumed for all GPUs : 0.000145 kWh. Total GPU Power : 5.2324 W\n",
      "[codecarbon INFO @ 17:03:00] 0.000170 kWh of electricity used since the beginning.\n",
      "[codecarbon INFO @ 17:03:00] 0.000024 g.CO2eq/s mean an estimation of 0.7601897238914499 kg.CO2eq/year\n",
      "[codecarbon INFO @ 17:03:07] Energy consumed for RAM : 0.000000 kWh. RAM Power : 0.0043544769287109375 W\n",
      "[codecarbon INFO @ 17:03:09] Energy consumed for all CPUs : 0.000027 kWh. Total CPU Power : 1.3187 W\n",
      "[codecarbon INFO @ 17:03:12] Energy consumed for all GPUs : 0.000146 kWh. Total GPU Power : 0.37899999999999995 W\n",
      "[codecarbon INFO @ 17:03:13] 0.000173 kWh of electricity used since the beginning.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "tracker = EmissionsTracker(save_to_api=True, tracking_mode=\"process\")\n",
    "model = \"llama2-uncensored:hot\" # You need to pull the model from the CLI\n",
    "n_poems = 10\n",
    "# Start tracking\n",
    "tracker.start()\n",
    "poems = []\n",
    "for i in range(n_poems):\n",
    "    response = ollama.chat(model=model, messages=[{'role': 'user', 'content': 'Write a poem for me about open source software'}])\n",
    "    poems.append(response['message']['content'])\n",
    "\n",
    "emmissions = tracker.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Source code is written with care,\\nAnd every line must be fair.\\nThe creator of the code,\\nThey offer it out wide and broad.\\nOpen source, I can see.\\nNo walls to stop the free.\\nIn this open source way,\\nI will find no dismay.\\nLet us all give it a try,\\nFor with this source code sky high.',\n",
       " 'The language that runs our devices, \\n\\nSoftly speaks without a noise or sign;\\n\\nOpen Source Software is its name. \\n\\nIts beauty lies within, hidden behind the flames; \\n\\nLike fireflies in a forest, twinkling light of code \\n\\nIs free for all, shared freely for its prowess and pride. \\n\\nA community working together to create a platform \\n\\nThat serves humanity with ease without any kind of control;\\n\\nIts users are the builders, shaping it as they like; \\n\\nThe source code open for everyone to look into their sight.\\n\\nIt runs our devices seamlessly and quietly. \\n\\nThe way its made, so much so we cannot deny, \\n\\nOpen Source Software, a treasure worth to behold.',\n",
       " \"In the realm of open source code, \\nWe programmers all share in a groove, \\nWhere innovation flows from our pens \\nAnd community spirit inspires us to wrench. \\nOur projects are born like little ones, \\nFrom humble beginnings or dreamy bons. \\nThen, we tend them and grow with each passing day \\nAnd soon enough they blossom into fay.\\nIn a world that's always on the move, \\nThe spirit of open source will prevail. \\nFor it offers us the tools to create, \\nAnd build solutions that break every tail.\",\n",
       " \"Open source software is an incredible force, \\n\\nBringing together programmers and other users from diverse communities, \\n\\nFreeing us of constraints to make anything that our imaginations dreams of, \\n\\nCollaborative work where each line shines brighter than before,\\n\\nComprehensive knowledge for free to share with anyone anywhere.\\n\\nNo one has the copyright or holds any patent on this powerful tool. \\n\\nOpen source is like an infinite canvas for artists without borders. \\n\\nCommunities unite together to create projects that no one owns, \\n\\nWhere each person adds something different but ultimately all belongs, \\n\\nContinuous innovation and progress for everyone's benefit and enjoyment. \\n\\nThis is the true beauty of open source software: it makes creation easier than ever before.\\n\\nAs more people get involved with it, we'll only see even greater wonders.\\n\\nThe power lies in collaboration - unleashing what has yet to come,\",\n",
       " \"The open-source code is like the sky without any clouds,\\nIt's where developers and hackers all come out of their holes.\\nYou can use it as you please, with no hidden agenda,\\nTo make the world a better place - just look at Linux 3.13.\\n\\nIn contrast to closed-source software, which keeps secrets for its creators' pleasure, open-source projects are a breath of fresh air for every coder or geek like me. With them, I don't have to worry about proprietary rights or licenses - they're freely available to anyone who wants them!\\n\\nWe share ideas, code, and solutions to problems; it's a collaborative process where everyone helps each other improve their products. Open-source developers believe that when everyone can see the code behind what they do, everyone will benefit from their contributions - not just themselves.\\n\\nAs a programmer, I can appreciate the importance of open source. Without its existence and support, I wouldn't be able to explore and develop as many skills and techniques without limitations imposed by a single vendor or owner who holds the patents to every piece of software they sell. Instead, we're free to contribute to and build upon others' ideas - something that has brought about so much creativity in the digital world over these past decades.\",\n",
       " \"Open Source Software \\nLike the air, I breathe every day, \\nFlowing freely in this open space, \\nWhere creativity flows with every passing ray. \\nNo more hidden costs, no need to pay, \\nFreely shared with everyone who's true, \\nTo support this spirit that will always move you.\",\n",
       " 'The software is open and free,\\nFor everyone to use as they please.\\nCommunities share ideas, innovations, and codes\\nTo create solutions for our problems.',\n",
       " \"Open source, like a breeze \\nThat brings forth ideas \\nIn an infinite recess of creative dreams. \\nSoftware developers share their vision,\\nFor a better digital connection \\nTo enrich the lives of many nations \\nThrough free-of-charge platforms and libraries \\n\\nLike waves upon an endless tide, \\nThese pieces come together to combine \\nInto innovative systems, that's not for profit. \\nWith open-source software, you get no fine print!\",\n",
       " 'Software is free, like the sky and the sun;\\nCode that runs forever, with no lock-in.\\nNo hidden agenda, just making life better,\\nMaking tools freely accessible for all.',\n",
       " \"In a sea of black and white, the lines of code glow brightly. \\nEach letter has meaning; each line has power. \\nA world where everyone can see, the beauty and magic that's in store.\\nNo walls, no paywalls to hide the source behind. \\nThis is a realm for dreamers, and we don't have time to wander aimlessly in a clouded mind.\\n\\nWe code and contribute because it feeds our need for expression.\\nOur vision united with others around us - a common ground we will create. \\nLet's not be afraid of making mistakes; learning is part of the game, right? \\nForget perfection; embrace progress! That's what this is all about!\"]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "poems"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'spreadsheetId': '1zGKLYhEkaD4R9l_Rhzs5blmWx608kA6BpzoTIPAO2zs',\n",
       " 'tableRange': 'Sheet1!A1:D42',\n",
       " 'updates': {'spreadsheetId': '1zGKLYhEkaD4R9l_Rhzs5blmWx608kA6BpzoTIPAO2zs',\n",
       "  'updatedRange': 'Sheet1!A43:D52',\n",
       "  'updatedRows': 10,\n",
       "  'updatedColumns': 4,\n",
       "  'updatedCells': 40}}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gspread\n",
    "from oauth2client.service_account import ServiceAccountCredentials\n",
    "import pandas as pd\n",
    "\n",
    "# Define the scope of the application\n",
    "scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']\n",
    "\n",
    "# Provide the path to your service account key\n",
    "creds = ServiceAccountCredentials.from_json_keyfile_name('/Users/luis/Downloads/mozfest-d60a0151e681.json', scope)\n",
    "client = gspread.authorize(creds)\n",
    "\n",
    "# Open the Google Sheet using its title or URL\n",
    "spreadsheet = client.open_by_url(\"https://docs.google.com/spreadsheets/d/1zGKLYhEkaD4R9l_Rhzs5blmWx608kA6BpzoTIPAO2zs/edit?usp=sharing\") \n",
    "sheet = spreadsheet.get_worksheet(0)\n",
    "\n",
    "def build_data_to_insert(poems, model):\n",
    "    data = {\n",
    "    'Poem': poems,\n",
    "    'Description': f\"Model: {model}, emissions: {emmissions / n_poems} kg/C02eq\",\n",
    "    \"Type\": \"SCALE\",\n",
    "    \"Answer Start\": \"0,5,BAD,GOOD\"\n",
    "    }\n",
    "    df =pd.DataFrame(data)\n",
    "\n",
    "    return df.astype(str).values.tolist()\n",
    "\n",
    "# Update the sheet with DataFrame data\n",
    "\n",
    "data_to_append = build_data_to_insert(poems, model)\n",
    "sheet.append_rows(data_to_append, value_input_option='RAW')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "codecarbon",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
